package view.room;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import entity.Room;
import entity.Booking;
import controllers.*;
public class RoomContent extends JPanel{
	private static final long serialVersionUID = 1L;
	RoomTable roomTable;
	RoomInfo  roomInfo;
	
	public RoomContent(){
		setLayout(new BorderLayout());
	
		add(roomTable = new RoomTable());
		add(roomInfo = new RoomInfo(), BorderLayout.SOUTH);
	}
	
	final class RoomTable extends JPanel{
		ArrayList<JButton> btRooms = new ArrayList<>();
		ArrayList<Room> room = RoomController.getAll();
                
		public RoomTable(){
                        room=RoomController.getAll();
			setLayout(new FlowLayout(FlowLayout.LEFT));
			refresh();
		}
		public void refresh(){
			btRooms.clear();
			removeAll();
			for (int i=0; i<room.size(); i++){
				RoomButton button = new RoomButton(room.get(i).getName());
                                System.out.println(room.get(i).getName());
				btRooms.add(button);
				button.addActionListener(new RoomSelectListener(i));
				add(button);
			}
			System.out.println("fresh room");
			repaint();
		}
		
		class RoomButton extends JButton{
			public RoomButton(String title){
				super(title);
				setPreferredSize(new Dimension(80, 80));
			}
		}
	}
	public void refreshAll(){
		roomTable.refresh();
	}
	public void filter(int type, int status){
		roomInfo.updateRoom(-1);
	}
	
	class RoomSelectListener implements ActionListener{
		int roomId;
		public RoomSelectListener(int index){
			roomId = index;
		}
		public void actionPerformed(ActionEvent event){
			roomInfo.updateRoom(roomId);
		}
	}
}
